<template>
    <page-wrap :loading="loading">
        <div class="store-container">
            <ul class="store-list" @click="changeAct">
                <li class="store-item" :class="{active:active == k}" v-for="v,k in orgList" :key="k" :data-id="k">{{v.name}}</li>
            </ul>
        </div>
        <template #footer>
            <div class="store-control">
                <z-button bold @click="onSubmit">进入店铺</z-button>
            </div>
        </template>
    </page-wrap>
</template>
<script setup>

import { showToast } from 'vant';
import useAppStore from '@/store/app'
import useConfigStore from '@/store/config'
import ls from '@/lib/ls'
import Api from '@/api'
import { validateMobile } from '@/lib/util.js'

import i_logo from '@/assets/images/account/logo.png'
import v_login_shop from '@/assets/svg/account/v_login_shop.svg'
import v_login_del from '@/assets/svg/account/v_login_del.svg'
import v_login_account from '@/assets/svg/account/v_login_account.svg'
import v_login_password from '@/assets/svg/account/v_login_password.svg'
import i_password_hide from '@/assets/images/account/i_password_hide.png'
import i_password_show from '@/assets/images/account/i_password_show.png'
import i_checkbox_check from '@/assets/images/account/i_checkbox_check.png'
import i_checkbox_checked from '@/assets/images/account/i_checkbox_checked.png'
import { Ls } from 'dayjs';
import { nextTick } from 'vue';

const configStore = useConfigStore()
const orgList = ls.get('STORE_LIST')
console.log(orgList);
const active = ref(0)
const router = useRouter()
const appStore = useAppStore()
const checked = ref(false)

const postForm = reactive({
    orgname: '',
    username: '',
    password: '',
})

const changeAct = (e)=>{
 const id = e.target.dataset.id
 active.value = id
}


const isAllValuesPresent = computed(() => {
    return Object.values(postForm).some(v => v == undefined || v == null || v == '');
});

const loading = ref(false)

const onSubmit = async () => {
    postForm.username = ls.get('LOGIN_VALUE').username
    postForm.password = ls.get('LOGIN_VALUE').password
    postForm.orgname = orgList[active.value].code
    ls.set('ORG_CODE',postForm.orgname)
    loading.value = true
    appStore.Login(postForm).then(() => {
        ls.set('BRAND_CODE',orgList[active.value].brandCode)
        loading.value = false
        router.replace({
            path: '/'
        })
        showToast('登录成功')
        // ls.remove('LOGIN_VALUE')
        let obj = ls.get('USER_INFO')
        obj.orgCode = postForm.orgname
        obj.orgName = orgList[active.value].name
        appStore.user_info.orgName = orgList[active.value].name
        
        ls.set('USER_INFO',obj)
        
        configStore.isLoad.state = 1
        console.log(configStore.isLoad);
    })
    .catch(()=>{
        ls.remove('ORG_CODE')
    })
};
// 一个门店默认登录
// if(orgList.length == 1){
//     onSubmit()
// }
const onInit = ()=>{
    const form = ls.get('LOGIN_FORM')
    if (form) {
        Object.assign(postForm, form);
        checked.value = true
    }
}
onInit()
</script>
<style scoped lang="scss">
.store-container {

    .store-list {
        padding: 0 12px;
    }
    .store-item {
        margin-top: 10px;
        padding: 22px 0 22px 12px;
        font-family: PingFang SC;
        font-weight: 500;
        font-size: 16px;
        color: #FFFFFF;
        line-height: 16px;
        background: #28282E;
        border-radius: 8px;
        &.active{
            border: 1px solid #FFDD99;
        }
    }
}
.store-control {
    padding: 0 12px 60px;
}
</style>